package com.luxsoft.siipap.em.replica.ui.tasks;

import java.awt.event.ActionEvent;
import java.text.MessageFormat;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

import org.apache.commons.lang.math.NumberUtils;

import com.jgoodies.binding.value.ValueHolder;
import com.jgoodies.forms.builder.ButtonBarBuilder;
import com.jgoodies.uif.AbstractDialog;
import com.jgoodies.uif.component.UIFButton;
import com.luxsoft.siipap.em.replica.importadores.inventarios.ImportadorDeTPE_TPS;
import com.luxsoft.siipap.em.replica.importadores.inventarios.ImportadorDeTPE_TPS2;
import com.luxsoft.siipap.model.Periodo;
import com.luxsoft.siipap.model.Sucursal;
import com.luxsoft.siipap.service.ServiceLocator2;
import com.luxsoft.siipap.swing.binding.Binder;
import com.luxsoft.siipap.util.DBUtils;

/**
 * Componente grafico para la importacion de clientes
 * 
 * @author Ruben Cancino
 *
 */
public class ImportadorDeTrasladosTask {

	public ImportadorDeTrasladosTask() {
		
	}
	
	public void execute(){		
		String res=JOptionPane.showInputDialog("Sucursal:");
		if(NumberUtils.isNumber(res)){
			Integer sucursal=Integer.valueOf(res);
			FacTaskDialog dialog=new FacTaskDialog();
			final SwingWorker worker=new Importador(dialog,sucursal.intValue());
			dialog.registerWorker(worker);
			dialog.open();
			printResult();
		}
		
	}
	
	protected void printResult(){
		
	}
	
	private class FacTaskDialog extends TaskDialog{
		
		protected Periodo periodo=Periodo.getPeriodoDelMesActual();
		
		public FacTaskDialog() {
			super("Importar de traslados");
		}
		
		protected JComponent buildButtonBar() {
			getHeader().setTitle("Periodo: "+periodo.toString());
			ButtonBarBuilder builder =new ButtonBarBuilder();
			builder.addGlue();
			builder.addGridded(createCambioPeriodoBtn());
			builder.addUnrelatedGap();
			builder.addGridded(createStartButton(true));
			builder.addRelatedGap();
			builder.addGridded(createStopButton());
	        builder.addUnrelatedGap();
	        builder.addGridded(createDetailButton());
	        return builder.getPanel();        
		}
		
		protected JButton createCambioPeriodoBtn(){
			Action cambiarPeriodo=new AbstractAction("Periodo"){
				public void actionPerformed(ActionEvent e) {
					ValueHolder holder=new ValueHolder(periodo);
					AbstractDialog dialog=Binder.createPeriodoSelector(holder);
					dialog.open();
					if(!dialog.hasBeenCanceled()){
						periodo=(Periodo)holder.getValue();
						getHeader().setTitle("Periodo: "+periodo.toString());
					}
					dialog.dispose();
					
				}
			};
			UIFButton btn=new UIFButton(cambiarPeriodo);
			return btn;
		}
		
	}
	
	private class Importador extends SwingWorker<String,String>{
		
		private final FacTaskDialog dialog;
		private final int sucursal;
		
		public Importador(final FacTaskDialog dialog,final int sucursal){
			this.dialog=dialog;
			this.sucursal=sucursal;
		}

		@Override
		protected String doInBackground() throws Exception {
			
			dialog.getHeader().setTitle("Periodo a importar: "+dialog.periodo);
			
			ImportadorDeTPE_TPS2 importador=new ImportadorDeTPE_TPS2();
			
			importador.importar(dialog.periodo.getFechaFinal(),sucursal);
			return "OK";
		}		

		@Override
		protected void done() {
			dialog.progress.setIndeterminate(false);
			
		}

		
		
	}
	
	public static void main(String[] args) {	
		DBUtils.whereWeAre();
		
		new ImportadorDeTrasladosTask().execute();
	}

}
